#include "nrf24l01.h"
#include "nrf24l01_head.h"
#include "Delay.h"

uint8_t t_buf[5] = { 0xF0, 0xF0,  0xF0, 0xF0, 0xF0 };
uint8_t r_buf[5] = { 0xF0, 0xF0,  0xF0, 0xF0, 0xF0 };

/* 开始定义读写NRF24L01的6个引脚的复位和置位函数 */
static void Sw_WriteMOSI(BitAction action)
{
    GPIO_WriteBit(MOSI_Port, MOSI_Pin, action);
}

static void Sw_WriteSCK(BitAction action)
{
    GPIO_WriteBit(SCK_Port, SCK_Pin, action);
}

static void Sw_WriteCSN(BitAction action)
{
    GPIO_WriteBit(CSN_Port, CSN_Pin, action);
}

static void Sw_WriteCE(BitAction action)
{
    GPIO_WriteBit(CE_Port, CE_Pin, action);
}

uint8_t Sw_ReadMISO(void)
{
    return GPIO_ReadInputDataBit(MISO_Port, MISO_Pin);
}

uint8_t Sw_ReadIRQ(void)
{
    return GPIO_ReadInputDataBit(IRQ_Port, IRQ_Pin);
}
/* 结束定义读写NRF24L01的6个引脚的复位和置位函数 */

/**
 * @brief NRF24L01调用SPI交换一个字节数据
 */
uint8_t Sw_SpiSwapByte(uint8_t Byte)
{
	uint8_t ByteReceive = 0x00;
	for(uint8_t i = 0; i < 8; i++) {
		Sw_WriteMOSI(Byte & ( 0x80 >> i));
		Sw_WriteSCK(Bit_SET);
		if (Sw_ReadMISO() == 1) {
			ByteReceive = ByteReceive | (0x80 >> i);
		}
		Sw_WriteSCK(Bit_RESET);
	}
	return ByteReceive;
}

/**
 * @brief 初始化NRF24L01,设置初始化时钟,设置引脚
 */
uint8_t Sw_ReadReg(uint8_t reg)
{
    uint8_t Value;
	Sw_WriteCSN(Bit_RESET);//表示选中NRF24L01
	Sw_SpiSwapByte(reg);//交换的第一个字节就是指令
	Value = Sw_SpiSwapByte(NOP);//交换的第二个字节就是交换的数据
	Sw_WriteCSN(Bit_SET);//停止选中NRF24L01
	return Value;
}
void Sw_WriteReg(uint8_t reg, uint8_t val)
{
    Sw_WriteCSN(Bit_RESET);//表示选中NRF24L01
	Sw_SpiSwapByte(reg);//交换的第一个字节就是指令
	Sw_SpiSwapByte(val);//交换的第二个字节就是交换的数据
	Sw_WriteCSN(Bit_SET);//停止选中NRF24L01
}

void Sw_WriteBuf(uint8_t reg, uint8_t *buf, uint8_t len)
{
    uint8_t i;
	Sw_WriteCSN(Bit_RESET);//选中NRF24L01
	Sw_SpiSwapByte(reg);
	for(i = 0; i < len; i++)
	{
		Sw_SpiSwapByte(buf[i]);
	}
	Sw_WriteCSN(Bit_SET);//停止选中NRF24L01
}
void Sw_ReadBuf(uint8_t reg, uint8_t *buf, uint8_t len)
{
    uint8_t i;
	Sw_WriteCSN(Bit_RESET);//选中NRF24L01
	Sw_SpiSwapByte(reg);
	for(i = 0; i < len; i++)
	{
		buf[i] = Sw_SpiSwapByte(NOP);
	}
	Sw_WriteCSN(Bit_SET);//停止选中NRF24L01
}

/**
 * @brief 初始化NRF24L01引脚
 */
void Sw_Nrf24L01InitPin(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
	
	//配置成推挽输出
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = MOSI_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(MOSI_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = SCK_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(SCK_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = CSN_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(CSN_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = CE_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(CE_Port, &GPIO_InitStructure);
	
	//配置成上拉输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = IRQ_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(IRQ_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = MISO_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(MISO_Port, &GPIO_InitStructure);
}

/**
 * @brief 初始化NRF24L01,设置初始化时钟,设置引脚
 */
void Sw_Nrf24L01Init(void)
{
    Sw_Nrf24L01InitPin();
    /* 拉低CE, 开启写使能 */
    Sw_WriteCE(Bit_RESET);

    Sw_WriteBuf(W_REGISTER+TX_ADDR, t_buf, 5);//配置发送地址
	Sw_WriteBuf(W_REGISTER+RX_ADDR_P0, r_buf, 5);//配置接收通道0
	Sw_WriteReg(W_REGISTER+CONFIG, 0x0F);//配置成接收模式
	Sw_WriteReg(W_REGISTER+EN_AA, 0x01);//通道0开启自动应答
	Sw_WriteReg(W_REGISTER+RF_CH, 0x00);//配置通信频率2.4G
	Sw_WriteReg(W_REGISTER+RX_PW_P0, 32);//配置接收通道0接收的数据宽度32字节
	Sw_WriteReg(W_REGISTER+EN_RXADDR, 0x01);//接收通道0使能
	Sw_WriteReg(W_REGISTER+SETUP_RETR, 0x1A);//配置580us重发时间间隔,重发10次
	Sw_WriteReg(FLUSH_RX, NOP);

    Sw_WriteCE(Bit_SET);
}

void Sw_RecvData(uint8_t *buf)
{
    uint8_t Status;
	Status =Sw_ReadReg(R_REGISTER+STATUS);
	if(Status & RX_OK)
	{
		Sw_ReadBuf(R_RX_PAYLOAD, buf, 32);
		Sw_WriteReg(FLUSH_RX,NOP);
		Sw_WriteReg(W_REGISTER+STATUS, Status);
		Delay_us(150);
	}
}
uint8_t Sw_SendData(uint8_t *buf)
{
    uint8_t Status;
	Sw_WriteBuf(W_TX_PAYLOAD, buf, 32);//在发送数据缓存器发送要发送的数据
	
	Sw_WriteCE(Bit_RESET);
	Sw_WriteReg(W_REGISTER+CONFIG,0x0E);
	Sw_WriteCE(Bit_SET);

	while(Sw_ReadIRQ()==1);//等待中断
	Status= Sw_ReadReg(R_REGISTER+STATUS);
	
	if(Status & MAX_TX)//如果发送达到最大次数
	{
		Sw_WriteReg(FLUSH_TX,NOP);//清除发送数据缓存器
		Sw_WriteReg(W_REGISTER+STATUS,Status);//中断位写1清除中断
		return MAX_TX;
	}
	if(Status & TX_OK)//如果发送成功,接收到应答信号
	{
		Sw_WriteReg(W_REGISTER+STATUS,Status);//清除中断
		return TX_OK;
	}
	return TX_OK;
}
